<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
include_once('Mysql_connect.inc');//启动连接，连接名：$conn
session_start();
$UserID = $_SESSION['ID'];
$UserName = $_SESSION['UserName'];
$CourseID = $_GET['courseid'];
//$UserID = '05177291244';
//$UserName = 'shawn';
//$CourseID = 'BC2045';
echo '<table>';
echo '<tr>';
echo '<th>course_id</th>';
echo '<th>subject</th>';
echo '</tr>';
$result = mysqli_query($conn, "SELECT course_id, subject FROM course WHERE course_id='$CourseID'");
while (($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) != NULL) {
    echo '<tr>';
    echo '<td>' . $row['course_id'] . '</td>';
    echo '<td>' . $subject = $row['subject'] . '</td>';
    echo '</tr>';
}
mysqli_free_result($result);
echo '</table>';

echo '<table>';
echo '<tr>';
echo '<th>student_card</th>';
echo '</tr>';
$result1 = mysqli_query($conn, "SELECT student_card FROM student WHERE student_id = '$UserID'");
while (($row1 = mysqli_fetch_array($result1, MYSQLI_ASSOC)) != NULL) {
    echo '<tr>';
    echo '<td>' . $student_card = $row1['student_card'] . '</td>';
    echo '</tr>';
}
mysqli_free_result($result1);
echo '</table>';
//echo $subject.' +  '.$student_card.' +  '.$UserName;

$result_select = mysqli_query($conn, "INSERT INTO registeredcourse
   (`course_id`, `subject`, `student_id`, `student_name`, `student_card`)
    VALUES
   ('$CourseID', '$subject', '$UserID', '$UserName', '$student_card')" );
if ($result_select) {
    echo 'You have select this course.';
}
else echo 'Error!';
mysqli_free_result($result_select);
include_once('Mysql_unconnect.inc'); //断开连接
?>
